//
//  MainTabBar.swift
//  Weibo19
//
//  Created by 城 on 15/11/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

   //重写构照方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        stopUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
   super.init(coder: aDecoder)


    }
    
    private func stopUI(){
        //添加加号按钮
        addSubview(composeBtn)
    }
    //布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置5个button的宽
        let w = self.bounds.width / 5
        //设置5个Butoon的宽
        let h = self.bounds.height
        //设置frame
        let rect = CGRectMake(0, 0, w, h)
//        记录当前是第一几个button
        var index :CGFloat = 0
        for subview in subviews{
            //判断是不是系统的UITabBarButton
     if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
        //设置CGRect偏移
         subview.frame = CGRectOffset(rect, w * index, 0)
        //三目运算,,当你是加号的时候,让后当第二的位置
        index += index == 1 ? 2:1
        
   }
}    //设置加号的frame
      composeBtn.frame = CGRectOffset(rect, w*2, 0)
        
    }
    
    //懒加载加好按钮
    lazy var composeBtn: UIButton = {
        //实例化Button
        let btn = UIButton()
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        //设置高亮背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        //设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
//        设置高亮图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        //设置图片原本的大小
        btn.sizeToFit()
        return btn
    }()
}



